<?php
session_start();
require 'Config.php';
$index = 0;
$countValue = NULL;
$arrConType = array();
$arrConValue = array();
$arrConPost = array();

/*
 * Check Condition
 */

    $arrConPost[1] = $_POST['occpu'];
    $arrConPost[2] = $_POST['country'];
    $arrConPost[3] = $_POST['sex'];
    $arrConPost[4] = $_POST['age'];
//
//    for($first=0;$first<$_POST['occpu'];$first++){
//        $arrConPost[1][$first] = $_POST['occpu'][$first];
//    }
//    
//    for($second=0;$second<$_POST['country'];$second++){
//        $arrConPost[2][$second] = $_POST['country'][$second];
//    }
//    
//    $arrConPost[3][0] = $_POST['sex'];
//    
//    for($third=0;$third<$_POST['age'];$third++){
//        $arrConPost[4][$third] = $_POST['age'][$third];
//    }

    //Check type of poll in db
    $check_type = "SELECT * FROM `condition` WHERE poll_id = ".$_POST['pollId']." GROUP BY condition_type";
    $rs = mysql_query($check_type)  or die("Error SQL[ ".$check_type." ] ".mysql_error());
    while ($type_db = mysql_fetch_array($rs)) {
        $arrConType[$index] = $type_db['condition_type'];
        $index++;
    }
    mysql_free_result($rs);
//    echo $arrConPost[1]."<br/>";
//    echo $arrConPost[2]."<br/>";
//    echo $arrConPost[3]."<br/>";
//    echo $arrConPost[4]."<br/>";
    //count type
//    echo "count : ".count($arrConType)."<br/>";
if(count($arrConType)>0){
    //loop for get each type
    $i=0;
    while($i<count($arrConType)){
        $arrTypeVal=null;
        $index2 =0;
        $get_type_val = "SELECT * FROM `condition` WHERE poll_id = ".$_POST['pollId']." AND condition_type = ".$arrConType[$i];
//        echo $get_type_val."<br/>";
        $rs = mysql_query($get_type_val)  or die("Error SQL[ ".$get_type_val." ] ".mysql_error());
        while ($type_val_db = mysql_fetch_array($rs)) {
            $arrTypeVal[$index2] = $type_val_db['condition_value'];
            $index2++;
        }
        mysql_free_result($rs);
//        for($z=0;$z<=count($arrTypeVal)-1;$z++){
//            echo "ค่าในดาต้าเบส : ".$arrTypeVal[$z]." : ค่าที่จะเอามาเช็ค ".$arrConPost[$arrConType[$i]]."<br/>";
//        }
        $result_array = in_array($arrConPost[$arrConType[$i]], $arrTypeVal); 
//        echo $arrConPost[$arrConType[$i]];
        if($result_array==TRUE){
            if($i==count($arrConType)-1){
                echo "<script>
                window.location.href = '../Interface/MenuAnswer.php?page=ViewPoll&pollId=".$_POST['pollId']."&poll=".$_POST['poll']."&occu=".$_POST['occpu']."&country=".$_POST['country']."&sex=".$_POST['sex']."&age=".$_POST['age']."&sid=".$_SESSION['ACCESS_PAGE_ID']."';
                </script>";
                $i++;
            }else{
                $i++;
            }
        }
        else
        {
             echo "<script>
            alert('เงื่อนไขคุณไม่เพียงพอในการสอบแบบสอบถาม');
            window.location.href = '../Interface/MenuAnswer.php';
            </script>";
            exit();
        }
        
    }
    mysql_close();
//    //Check type
//    if(count($arrConType)!=0){
//        $countValue = 0;
//        for($i = 0;$i<count($arrConType);$i++){
//            if($arrConPost[$arrConType[$i]] == $arrConValue[$i]){
//               $countValue++;
//            }
//        }
//    }
//    
//    //Check value
//    if($countValue!=NULL){
//        if($countValue!=count($arrConType)){
//            echo "<script>
//            alert('เงื่อนไขคุณไม่เพียงพอ');
//            window.location.href = '../Interface/MenuAnswer.php';
//            </script>";
//        }else{
//            echo "<script>
//            window.location.href = '../Interface/MenuAnswer.php?page=ViewPoll&pollId=".$_POST['pollId']."&poll=".$_POST['poll']."&occu=".$_POST['occpu']."&country=".$_POST['country']."&sex=".$_POST['sex']."&age=".$_POST['age']."';
//            </script>";
//        }
//    }
}else{
    echo "<script>
    window.location.href = '../Interface/MenuAnswer.php?page=ViewPoll&pollId=".$_POST['pollId']."&poll=".$_POST['poll']."&occu=".$_POST['occpu']."&country=".$_POST['country']."&sex=".$_POST['sex']."&age=".$_POST['age']."&sid=".$_SESSION['ACCESS_PAGE_ID']."';
    </script>";
}

?>
